import 'dart:math';

import 'package:get/get.dart';

const kUpperCaseLetter = 'U';
const kLowerCaseLetter = 'L';
const kArabicNumerals = 'A';
const kSpecialSymbol = 'S';

class PasswordGeneratorLogic extends GetxController {

  final minLength = 6;
  final maxLength = 30;
  static final _specialSymbolStrings = '!@#\$%^&*';

  var length = 6.obs;
  var upperCaseLetter = true.obs;
  var lowerCaseLetter = true.obs;
  var arabicNumerals = true.obs;
  var specialSymbol = true.obs;

  var _password = ''.obs;

  final _upperCaseLetterChars = List.generate(26, (index) => String.fromCharCode('A'.codeUnitAt(0) + index));
  final _lowerCaseLetter = List.generate(26, (index) => String.fromCharCode('a'.codeUnitAt(0) + index));
  final _arabicNumeralsChars = List.generate(10, (index) => index.toString());
  final _specialSymbols = _specialSymbolStrings.split('');

  PasswordGeneratorLogic(){
    generate();
    ever(length, (value) => generate());
    ever(upperCaseLetter, (value) => generate());
    ever(lowerCaseLetter, (value) => generate());
    ever(arabicNumerals, (value) => generate());
    ever(specialSymbol, (value) => generate());
    ''.split('');
  }

  void generate(){
    Map<String, List<String>> wordsMap = {};
    if(upperCaseLetter.value){
      wordsMap[kUpperCaseLetter] = _upperCaseLetterChars;
    }
    if(lowerCaseLetter.value){
      wordsMap[kLowerCaseLetter] = _lowerCaseLetter;
    }
    if(arabicNumerals.value){
      wordsMap[kArabicNumerals] = _arabicNumeralsChars;
    }
    if(specialSymbol.value){
      wordsMap[kSpecialSymbol] = _specialSymbols;
    }

    var result = '';
    List<String> positions = [];
    for(int i=0; i< length.value; i++){

      result += _randomString(items[i%items.length]);
    }

    _password.value = result;
  }

  String _randomString(List<String> chars){
    return chars[Random.secure().nextInt(chars.length)];
  }

  String get password => _password.value;
  String get specialSymbolString => _specialSymbolStrings;
}
